/*************************************************************************

Title:    example program for the Interrupt controlled UART library

Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury

File:     $Id: test_uart.c,v 1.4 2005/07/10 11:46:30 Peter Exp $

Software: AVR-GCC 3.3

Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz



DESCRIPTION:

          This example shows how to use the UART library uart.c



*************************************************************************/

#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay_basic.h>




#include "uart.h"
#include "dataDef.h"
#include "util.h"
#include "gpio.h"


/* define CPU frequency in Mhz here if not defined in Makefile */

#ifndef F_CPU

#define F_CPU 3686400

#endif



/* 9600 baud */

#define UART_BAUD_RATE      9600      

int main(void)
{

    DDRA  = 0xFF;                // use all pins on PortA for input 

	DDRD = 0b00000011;

	PORTA = ~PIND;

/*
    PORTA = 0x00;                // and turn off all LEDs

	port porta;

	porta.Bits.bit0 = 1;
	porta.Bits.bit1 = 1;
	porta.Bits.bit2 = 1;
	porta.Bits.bit3 = 0;
	porta.Bits.bit4 = 0;
	porta.Bits.bit5 = 0;
	porta.Bits.bit6 = 0;
	porta.Bits.bit7 = 0;

	PORTA = porta.Byte;

	*/

	unsigned char c =0;
  
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); 

   
    sei();

	 unsigned char dataOut[8] = {0};
	 unsigned char dataIn[8] = {0};	 

	mbPacket testPacket;

	testPacket.header.slaveAddress = 0xA;
	testPacket.header.op = 0x03;
	testPacket.tail.readPacketRequest.startWord.Bytes.ByteHi = 0x00; 
	testPacket.tail.readPacketRequest.startWord.Bytes.ByteLo = 0x00;
	testPacket.tail.readPacketRequest.numWord.Bytes.ByteHi = 0x00;
	testPacket.tail.readPacketRequest.numWord.Bytes.ByteLo = 0x01;

	testPacket.tail.readPacketRequest.CRC.Word = calculateMBcrc(testPacket,read_request);



	uart_puts((const char *)serializeMBpacket(testPacket,dataOut));


	_delay_loop_2(65536);


	read(dataIn,expectedLenght(testPacket,read_request));
	
	_delay_loop_2(65536);
	_delay_loop_2(65536);


	write(dataIn,expectedLenght(testPacket,read_request));

	//porta.Byte = dataIn[3];

//	PORTA = porta.Byte;


	_delay_loop_2(65536);
	_delay_loop_2(65536);


}

void write(unsigned char * dataOut, int len)
{
	int i = 0;
	for(i=0;i<len-1;i++)
	{
		uart_putc(dataOut[i]);
	}

}


int read(unsigned char * dataIn, int len)
{

	int c =0;

	int dataCount = 0;
	while(dataCount < len-1)
	{
	
	c = uart_getc();

        if ( c & UART_NO_DATA ) { }

        else
        {

            if ( c & UART_FRAME_ERROR )

            {
                uart_puts_P("UART Frame Error: ");
            }

            if ( c & UART_OVERRUN_ERROR )
            {
                uart_puts_P("UART Overrun Error: ");

            }

            if ( c & UART_BUFFER_OVERFLOW )
            {
                uart_puts_P("Buffer overflow error: ");
            }
			
			dataIn[dataCount] = (unsigned char) c;
			dataCount = dataCount++;
        }
	}
}

